home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / make / icmake-6.000 / icmake-6 / icmake / rss / getstrin.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-08  |  1.9 KB  |  73 lines

  1. /*
  2. \funcref{getstring}{char $*$getstring (\params)}
  3.     {
  4.         {FILE} {*f} {binary file to read from}
  5.         {INT32} {stringsec} {offset of string section within file}
  6.         {UNS16} {stringofs} {offset of string within string section}
  7.     }
  8.     {pointer to read string, or --1 when reading failed}
  9.     {}
  10.     {getopcode()}
  11.     {getstrin.c}
  12.     {
  13.         Function {\em getstring()} can be used to retrieve a string from the
  14.         binary makefile. The string is identified by the offset of the string
  15.         section within the file (the first {\em INT32} value in the file) and
  16.         by the offset of the string itself within the string section.
  17.  
  18.         {\em getstring()} returns a pointer to allocated memory, holding the
  19.         read string. The caller is responsible for freeing this memory.
  20.  
  21.         Value --1 is returned when the reading failed; i.e., when file
  22.         positioning failed or when no string was found at the specified
  23.         position.
  24.     }
  25.  
  26. Example:
  27. {\footnotesize
  28.     \begin{verbatim}
  29.         // assuming 'infile' is opened for the binary file,
  30.         // show the first string in the file (if any)
  31.         INT32
  32.             stringsection;
  33.  
  34.         rewind (infile);
  35.         fread (&stringsection, sizeof (INT32), 1, infile);
  36.         puts (getstring (infile, stringsection, 0));
  37.     \end{verbatim}
  38. } % end footnotesize
  39. */
  40.  
  41.  
  42. #include "icrssdef.h"
  43. #include "../icm.h"
  44.  
  45. char *getstring (FILE *f, INT32 stringsec, UNS16 stringofs)
  46. {
  47.     INT32
  48.         curoffs;
  49.     char
  50.         buf [80];
  51.     register char
  52.         *ret = NULL;
  53.     register int
  54.         done = 0;
  55.  
  56.     curoffs = ftell (f);
  57.  
  58.     if (fseek (f, stringsec + stringofs, SEEK_SET))
  59.         return ( (char *) -1 );
  60.  
  61.     while (! done)
  62.     {
  63.         if (! fgetz (buf, 79, f))
  64.             return ( (char *) -1);
  65.         ret = xstrcat (ret, buf);
  66.         if (strlen (buf) < 78)
  67.             done++;
  68.     }
  69.  
  70.     fseek (f, curoffs, SEEK_SET);
  71.     return (ret);
  72. }
  73.